﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
namespace DAO
{
    public class HoaDonDAO
    {
        public DataTable LayTable()
        {
            String sql = "SELECT H.MaHoaDon,H.MaKhachHang,TenKhachHang,NgayLap,SoDT,DiaChi from HoaDon H,KhachHang K where H.MaKhachHang = K.MaKhachHang ";
            SqlConnection con = DataProvider.ConnectDB();

            SqlCommand command = new SqlCommand(sql, con);
            con.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(command);

            DataTable dt = new DataTable();
            adapter.Fill(dt);

            con.Close();
            return dt;
        }
        public DataTable LayTableTHeoKhacHang(int maKH)
        {

            String sql = "SELECT H.MaHoaDon,H.MaKhachHang,TenKhachHang,NgayLap,SoDT,DiaChi from HoaDon H,KhachHang K where H.MaKhachHang = K.MaKhachHang and H.MaKhachHang = " + maKH;
            SqlConnection con = DataProvider.ConnectDB();

            SqlCommand command = new SqlCommand(sql, con);
            con.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(command);

            DataTable dt = new DataTable();
            adapter.Fill(dt);

            con.Close();
            return dt;
        }
      
        public DataTable LayTableTHeongay(DateTime form, DateTime to)
        {
            String sql = "SELECT * from HoaDon where NgayLap >= '" + form + "' and NgayLap <= '" + to + "'";
            SqlConnection con = DataProvider.ConnectDB();

            SqlCommand command = new SqlCommand(sql, con);
            con.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(command);

            DataTable dt = new DataTable();
            adapter.Fill(dt);

            con.Close();
            return dt;
        }
        public void Xoa(HoaDonDTO hd)
        {
            String sql = "DELETE from HoaDon  where MaHoaDon =" + hd.MaHoaDon;
            SqlConnection con = DataProvider.ConnectDB();
            con.Open();
            SqlCommand command = new SqlCommand(sql, con);
            int res = -1;
            res = command.ExecuteNonQuery();
            con.Close();
        }
        public DataTable LayTableNguoc()
        {
            String sql = "SELECT top 1 * from HoaDon order by MaHoaDon desc";
            SqlConnection con = DataProvider.ConnectDB();
            con.Open();
            SqlCommand command = new SqlCommand(sql, con);

            SqlDataAdapter adapter = new SqlDataAdapter(command);

            DataTable dt = new DataTable();
            adapter.Fill(dt);

            con.Close();
            return dt;
        }
        public void them(HoaDonDTO hd)
        {
            String sql = "Insert into HoaDon(MaKhachHang,NgayLap) values(" + hd.MaKhachHang + ",'" + hd.NgayLap + "')";

            SqlConnection con = DataProvider.ConnectDB();
            con.Open();
            SqlCommand command = new SqlCommand(sql, con);
            int res = -1;
            res = command.ExecuteNonQuery();
            con.Close();
        }
    }
}
